/*
 * 文件名：UserDaoImp.java
 * 版权：Copyright by www.wootide.com
 * 描述：
 * 修改人：ozm
 * 修改时间：2013-1-24
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */
package com.wootide.cms.service.facility.room.table.dao.imp;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wootide.cms.common.QueryCondition;
import com.wootide.cms.common.QueryResults;
import com.wootide.cms.log.syslog.RunLog;
import com.wootide.cms.service.facility.room.table.dao.TableNoInfoDao;
import com.wootide.cms.service.facility.room.table.pojo.TableNoInfo;
import com.wootide.cms.service.reservation.orderby.dao.OrderByInfoDao;
import com.wootide.cms.service.reservation.orderby.pojo.OrderByInfo;

public class TableNoInfoDaoImp extends HibernateDaoSupport implements TableNoInfoDao
{
    //注入运行日志
    private RunLog runLog;
    
  
    
   

    /**
     * 通过订单号获取所有的房间信息
     * Description: <br>
     * Implement: <br>
     * @param orderId 订单ID
     * @return
     * @see
     * @return List 房间列表
     * @author ozm2013-4-7下午5:56:14
     */
    public List<?> queryRoomsByOrderId(Integer orderId)
    {
        runLog.debug("orderId" + orderId);
        return this.getHibernateTemplate().findByNamedQuery("findTablePeriodRoomByBookOrderInfoId", orderId);
    }

    /**
     * 通过订单ID和房间ID获取桌次信息
     * Description: <br>
     * Implement: <br>
     * @param orderId  订单ID
     * @param roomId  房间ID
     * @return
     * @see 
     * @return List<?> 桌次列表
     * @author ozm2013-4-7下午5:57:21
     */
    public List<?> queryTableNoInfosByOrderIdAndRoomId(Integer orderId,Integer roomId)
    {
        runLog.debug("orderId" + orderId);
        runLog.debug("roomId" + roomId);
        Object[] values = {orderId,roomId};
        return this.getHibernateTemplate().findByNamedQuery("findTableNoInfoByBookOrderInfoIdAndRoomId", values);
    }
    
    
    /**
     * 通过订单索引和桌次索引查询时段信息
     * Description: <br>
     * Implement: <br>
     * @param orderId 订单索引
     * @param tableId 桌次索引
     * @return
     * @see
     * @return List<?> 时段记录集合
     * @author ozm2013-4-8上午9:57:54
     */
    public List<?> queryTablePeriodByOrderIdAndTableId(Integer orderId,Integer tableId)
    {
        runLog.debug("orderId" + orderId);
        runLog.debug("tableId" + tableId);
        Object[] values = {orderId,tableId};
        return this.getHibernateTemplate().findByNamedQuery("findTablePeriodByBookOrderInfoIdAndTableId", values);
    }
    
    /**
     * 通过起止时间和桌次信息查询时段信息
     * Description: <br>
     * Implement: <br>
     * @param startTime 起始时间
     * @param endTime  结束时间
     * @param tableId   房间ID
     * @return
     * @see
     * @return List<?> 结果集
     * @author ozm2013-4-11下午6:05:13
     */
    public List<?> queryPeriodsByTableIdAndStartTimeAndEndTime(
            Object startTime, Object endTime, Object tableId)
    {
        
        runLog.debug("startTime=" + startTime);
        runLog.debug("endTime=" + endTime);
        runLog.debug("roomId=" + tableId);
        
//        Object[] values = {tableId,startTime,endTime};
        String hql = "from TablePeriod p where  p.tableNoInfo.innerIndex = " + tableId +" and p.startTime >= '" + startTime + "'  and  p.endTime <= '" +  endTime + "'";
        return this.getHibernateTemplate().find(hql);
    }
    
    /**
     * 通过id获取桌次信息
     * Description: <br>
     * Implement: <br>
     * @param tableId 桌次ID
     * @return
     * @see
     * @return TableNoInfo 桌次信息
     * @author ozm2013-4-12下午4:02:18
     */
    public TableNoInfo findOneTableNoInfoById(Integer tableId)
    {
        runLog.debug("tableId=" + tableId);
        return this.getHibernateTemplate().get(TableNoInfo.class, tableId);
    }
    
    /**
     * 通过索引列表和订单ID获取桌次时段记录
     * Description: <br>
     * Implement: <br>
     * @param orderId 订单索引
     * @param innerIndexs 桌次时段索引集合
     * @return
     * @see
     * @return List<?> 结果集合
     * @author ozm2013-4-12下午5:53:42
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public List<?> queryTablePeriodsInIdListByOrderId(final Integer orderId,
            final Integer[] innerIndexs)
    {
        return getHibernateTemplate().execute(new HibernateCallback()
        {
            public List doInHibernate(Session session)
                    throws HibernateException, SQLException
            {
                
                String hql = "";
                Query query = null;
                hql = "from TablePeriod p where  p.innerIndex in (:innerIndexs) ";
//                hql = "from RoomPeriod p where p.bookOrderInfo.innerIndex != "+ orderId + " and p.innerIndex in (:innerIndexs) ";
                query = getSession().createQuery(hql);
                query.setParameterList("innerIndexs",innerIndexs);
                return query.list();
            }
        });
    }

    
    /**
     * 通过索引列表和订单ID获取被取消的桌次时段记录
     * Description: <br>
     * Implement: <br>
     * @param orderId 订单索引
     * @param innerIndexs 桌次时段索引集合
     * @return
     * @see
     * @return List<?> 结果集
     * @author ozm2013-4-12下午5:54:55
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public List<?> queryTablePeriodsNotInIdListByOrderId(final Integer orderId,
            final Integer[] innerIndexs)
    {
        return getHibernateTemplate().execute(new HibernateCallback()
        {
            public List doInHibernate(Session session)
                    throws HibernateException, SQLException
            {
                
                String hql = "";
                Query query = null;
                hql = "from TablePeriod p where p.bookOrderInfo.innerIndex = "+ orderId + " and p.innerIndex not in (:innerIndexs) ";
                query = getSession().createQuery(hql);
                query.setParameterList("innerIndexs",innerIndexs);
                return query.list();
            }
        });
    }
    
    public RunLog getRunLog()
    {
        return runLog;
    }
    
    public void setRunLog(RunLog runLog)
    {
        this.runLog = runLog;
    }
}
